package cn.yunhe.crm.service;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.yunhe.crm.dao.BusinessMapper;
import cn.yunhe.crm.entity.Business;
import java.util.HashMap;
import java.util.Map;



@Service("BusinessService")
public class BusinessService {
	@Autowired
	private BusinessMapper bMapper;

	public void setbMapper(BusinessMapper bMapper) {
		this.bMapper = bMapper;
	}
	/**
	 * 获取所有的商机信息
	 * @return
	 */
	public List<Business> getList() {
		List<Business> list = bMapper.findBusinessAndOrg();
		return list;
	}
	/**
	 * 添加商机
	 * @param business
	 * @return
	 */
	public boolean addBusiness(Business business) {
		int r = bMapper.insertSelective(business);
		return r>0?true:false;
	}
	/**
	 * 根据id查询商机
	 * @param id
	 * @return
	 */
	public Business findBusinessById(Integer id) {
		Business business = bMapper.findByPrimaryKey(id);
		return business;
	}
	/**
	 * 更新商机
	 * @param business
	 * @return
	 */
	public boolean updateBusiness(Business business) {
		int r = bMapper.updateByPrimaryKeySelective(business);
		return r>0?true:false;
	}
	/**
	 * 删除商机
	 * @param id
	 * @return
	 */
	public boolean delBusiness( Integer id) {
		Business b = bMapper.selectByPrimaryKey(id);
		b.setDelstate(1);
		int r = bMapper.updateByPrimaryKeySelective(b);
		return r>0?true:false;
	}
	
	
	/**
	 * 查询商机总数
	 * @return
	 */
	public int checkCount(){
		return bMapper.countByExample(null);
	}
	/**
	 * 根据时间查询商机总数
	 * @param begintime
	 * @param endtime
	 * @return
	 */
	public int checkCountByYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return bMapper.checkCountByYearAndMonth(map);
	}
	/**
	 * 查询不同阶段状态的商机总数
	 * @param stage
	 * @return
	 */
	public int checkCountByStage(Integer stage){
		return bMapper.checkCountByStage(stage);
	}
	/**
	 * 根据时间查询不同阶段状态的商机总数
	 * @param stage
	 * @return
	 */
	public int checkCountByStageByYearAndMonth(Integer stage,String begintime,String endtime){
		Map map=new HashMap();
		map.put("stage", stage);
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return bMapper.checkCountByStageByYearAndMonth(map);
	}
	/**
	 * 通过商机赢单状态查询机构oid
	 * @return
	 */
	public List<Integer> checkOidByStage(){
		return bMapper.checkOidByStage();
	}
	/**
	 * 根据时间通过商机赢单状态查询机构oid
	 * @return
	 */
	public List<Integer> checkOidByStageByYearAndMonth(String begintime,String endtime){
		Map map=new HashMap();
		map.put("begintime", begintime);
		map.put("endtime", endtime);
		return bMapper.checkOidByStageByYearAndMonth(map);
	}
	
	public int countByToday(){
	 	   
	     return bMapper.checkCountByToday();
	}

}
